package com.woniuxy.woniuticket.statisticsmodule.service.impl;

import com.woniuxy.woniuticket.moviemodule.dao.CinemaHallDAO;
import com.woniuxy.woniuticket.moviemodule.dao.CinemaMoviesDAO;
import com.woniuxy.woniuticket.moviemodule.dao.CinemaScheduleDAO;
import com.woniuxy.woniuticket.ordermodule.dao.CinemaOderDAO;
import com.woniuxy.woniuticket.ordermodule.dao.CinemaStreamDAO;
import com.woniuxy.woniuticket.ordermodule.dao.MoviesCommentDAO;
import com.woniuxy.woniuticket.pojo.CinemaHall;
import com.woniuxy.woniuticket.pojo.CinemaMovies;
import com.woniuxy.woniuticket.pojo.CinemaSchedule;
import com.woniuxy.woniuticket.pojo.CinemaStream;
import com.woniuxy.woniuticket.statisticsmodule.service.StatisticsService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 赵晓东 on 2019/6/5/005.
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Resource
    private CinemaOderDAO cinemaOderDao;

    @Resource
    private CinemaMoviesDAO cinemaMoviesDAO;

    @Resource
    private CinemaScheduleDAO cinemaScheduleDAO;

    @Resource
    private MoviesCommentDAO moviesCommentDAO;

    @Resource
    private CinemaHallDAO cinemaHallDAO;

    @Resource
    private CinemaStreamDAO cinemaStreamDAO;

    //日期转换
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    //查询营业额
    @Override
    public Map<String,BigDecimal> findturnover(Date startDay,Date endDay){
        String str;
        Date time = null;
        Integer num = 0;
        //获取日历对象
        Calendar calendar = Calendar.getInstance();
        //天数的增减
        int upordown = 0;
        //传入的开始日期和结束日期都不为空
        if(startDay!=null && endDay != null) {
            //计算天数差
            num = (int) ((endDay.getTime() - startDay.getTime()) / (1000 * 60 * 60 * 24));
            num = num + 1;
            //载入日历
            calendar.setTime(startDay);
            //天数增加
            upordown = 1;
        }else if (startDay != null && endDay == null){
            num = 5;
            //载入日历
            calendar.setTime(startDay);
            //天数增加
            upordown = 1;
        }else if (startDay == null && endDay != null){
            num = 5;
            //载入日历
            calendar.setTime(endDay);
            //天数增加
            upordown = -1;
        }

        //营业额集合
        Map<String,BigDecimal> map = new TreeMap<>();

        for (int i = 0; i < num; i++) {
            //得到时间
            time = calendar.getTime();
            str = transFormString(time);
            //流水
            BigDecimal turnWater = new BigDecimal(0);
            //每一天的收入
            BigDecimal income = cinemaStreamDAO.selectTurnWater(0, str);
            //每一天的支出
            BigDecimal pay = cinemaStreamDAO.selectTurnWater(1, str);
            if(income==null){
                income = new BigDecimal(0);
            }
            if(pay==null){
                pay = new BigDecimal(0);
            }
            turnWater = income.subtract(pay);

            map.put(str,turnWater);
            //向前推一天
            calendar.add(Calendar.DATE,upordown);
        }
        return map;
    }
    //查询电影排片量
    @Override
    public Map<String, Integer> findPlanMovie() {
        //先查询电影信息
        List<CinemaMovies> movies = cinemaMoviesDAO.findAllMovies();
        //影片id
        Integer id;
        //排片量
        Integer num;
        Map<String,Integer> map = new TreeMap<>();
        for (CinemaMovies movice:
                movies) {
            id = movice.getId();
            num = cinemaScheduleDAO.findNumByMovie(id);
            map.put(movice.getMovename(),num);
        }
        return map;
    }
    //电影评分
    @Override
    public double avgStar(int movieId) {
        if(moviesCommentDAO.findCommentsByMovieId(movieId).size()!=0) {
            return moviesCommentDAO.selectAvgStarByMovieId(movieId);
        }
        return 0.0;
    }

    //电影上座率
    @Override
    public Map<String, Double> upperSeat() {
        //先查询电影信息
        List<CinemaMovies> movies = cinemaMoviesDAO.findAllMovies();
        //电影座位总数
        double seatnum = 0;
        //卖出电影票
        double haveseat = 0;
        //数据集合
        Map<String,Double> map = new TreeMap<>();
        //遍历所有电影
        for (CinemaMovies movice:
                movies) {
            //每部电影的排片情况
            List<CinemaSchedule> list = cinemaScheduleDAO.findScheduleByMovie(movice.getId());
            //ES自定义 根据id查询
            //MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("movies_id",movice.getId());
            //Iterable<CinemaSchedule>list = this.statistcsRepository.search(matchQueryBuilder);

            for (CinemaSchedule schedule:
                    list) {
                //电影座位总数
                seatnum += cinemaHallDAO.selectByPrimaryKey(schedule.getHallId()).getSeatsnum();
                //卖出电影票
                haveseat += cinemaOderDao.findOrdersBySchedule(schedule.getId());
            }
            map.put(movice.getMovename(),(haveseat/seatnum));
            //将已选座位清空
            seatnum = 0;
            haveseat = 0;
        }
        return map;
    }

    //日期转换字符串方法
    public String transFormString(Date time) {
        return simpleDateFormat.format(time);
    }

    //字符串转换日期方法
    public Date transFormDate(String str) throws ParseException {
        return simpleDateFormat.parse(str);
    }
}
